from __future__ import absolute_import
import xadmin
from .models import UserSettings, Log
from xadmin.layout import *

from django.utils.translation import ugettext_lazy as _, ugettext

from xadmin import views


class GlobalSetting(object):
    site_title = "Msquare"  # 设置标题
    site_footer = "this is a about movie web"  # 设置底部文字
    menu_style = "accordion"  # 使左侧菜单列表为伸缩样式

xadmin.site.register(views.CommAdminView, GlobalSetting)  # views.CommAdminView这是xadmin自带的类


class BaseSetting(object):
    enable_themes = True  # 添加主题选择功能
    use_bootswatch = True  # 添加多个主题到选择中


xadmin.site.register(views.BaseAdminView, BaseSetting)


class UserSettingsAdmin(object):
    model_icon = 'fa fa-cog'
    hidden_menu = True

xadmin.site.register(UserSettings, UserSettingsAdmin)

class LogAdmin(object):

    def link(self, instance):
        if instance.content_type and instance.object_id and instance.action_flag != 'delete':
            admin_url = self.get_admin_url('%s_%s_change' % (instance.content_type.app_label, instance.content_type.model), 
                instance.object_id)
            return "<a href='%s'>%s</a>" % (admin_url, _('Admin Object'))
        else:
            return ''
    link.short_description = ""
    link.allow_tags = True
    link.is_column = False

    list_display = ('action_time', 'user', 'ip_addr', '__str__', 'link')
    list_filter = ['user', 'action_time']
    search_fields = ['ip_addr', 'message']
    model_icon = 'fa fa-cog'

xadmin.site.register(Log, LogAdmin)
